package me.zd.tool.hardlink.view.listener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.function.Consumer;

public class NumberSpinnerListener implements ChangeListener {
    private final JSpinner component;
    private final Consumer<Number> changedOperation;
    private final ModelLock modelLock;

    public NumberSpinnerListener(JSpinner component, Consumer<Number> changedOperation, ModelLock modelLock) {
        this.component = component;
        this.changedOperation = changedOperation;
        this.modelLock = modelLock;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (!modelLock.isLocked()) {
            changedOperation.accept((Number) component.getValue());
        }
    }
}
